package day11;

import java.util.concurrent.locks.ReentrantLock;

//测试死锁：实现两个线程 持有多把锁 让其相互又对方的锁
public class TestDeadLock {
    public static void main(String[] args) {
        String s1 = "java";
        String s2 = "mysql";
        new Thread(() -> {
            synchronized (s1) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1获取了S1的锁，等待获取S2的锁");
                synchronized (s2) {
                    System.out.println("线程1获取了S1和S2的锁");
                }
                System.out.println("线程1释放了S2的锁");
            }
            System.out.println("线程1释放了S1的锁");
        }).start();

        new Thread(() -> {
            synchronized (s2) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程2获取了S2的锁，等待获取S1的锁");
                synchronized (s1) {
                    System.out.println("线程2获取了S1和S2的锁");
                }
                System.out.println("线程2释放了S1的锁");
            }
            System.out.println("线程2释放了S2的锁");
        }).start();
    }
}
